﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace GAscheduler
{
    public class RuleToken
    {
        public enum TokenType
        {
            ERROR = 0, RM, CL, BD, CSCI, 
            // 4
            ARTH, ART, BIO, CHEM, FLAN, 
            // 9
            LIT, ENGL, HIST, REL, SOC,
            // 14
            PHIL, BNR, MUS, MATH, SC, SN,
            // 20
            SG, VNY, RDFD, DVS, T, F,
            // 26
            COMMA, LEFT_BRAC, RIGHT_BRAC,
            // 29
            DOT, PHYS, COURSE
            // 32
        }; // TokenType

        private TokenType m_Type;    // type of token
        private string m_Name;       // string representation of the token
        private int m_TokenNumber;   // token prototype number
        private Hashtable c_TokenTable = new Hashtable(); // a hashtable containing most of the keywords/symbols encountered while Lexing

        /// <summary>
        /// Constructor for the token class. This populates a static hashtable with 
        ///     keywords/symbols, and uses that to crosscheck the incomming TokenName 
        ///     to see if it is a valid token. If that doesn't work, the token is 
        ///     treated as a course. NOTE: course and classroom are rather 
        ///     indistinguishable, so this code will treat them the same
        /// </summary>
        /// <param name="TokenName"></param>
        public RuleToken(string TokenName)
        {
            PopulateHashTable();

            if (c_TokenTable.Contains(TokenName)) // if we can find the token in the hashtable,
            {
                m_Name = TokenName;               // assign its appropriate members
                m_TokenNumber = (int)c_TokenTable[TokenName];
                m_Type = (TokenType)m_TokenNumber;
            }
            else                                  // otherwise, it must be a course (or classroom)
            {
                m_Name = TokenName;
                m_Type = TokenType.COURSE;
                m_TokenNumber = 32;
            }
        } // RuleToken
        
        /// <summary>
        /// Accessor function. Returns the string representation of the token.
        /// </summary>
        /// <returns></returns>
        public string GetTokenName()
        {
            return m_Name;
        } // GetTokenName

        /// <summary>
        /// Accessor function. Returns the token prototype.
        /// </summary>
        /// <returns></returns>
        public TokenType GetTokenType()
        {
            return m_Type;
        } // GetTokenType

        /// <summary>
        /// Accessor function. Returns the number associated with the token 
        ///     prototype of this token.
        /// </summary>
        /// <returns></returns>
        public int GetTokenNumber()
        {
            return m_TokenNumber;
        } // GetTokenNumber

        /// <summary>
        /// Populates the hashtable using a static function that is supposed to 
        ///     only happen once. After that, any new tokens can be easily 
        ///     crosschecked with this list to make sure they are valid.
        /// </summary>
        public void PopulateHashTable()
        {
            if (c_TokenTable.Count == 0) // make sure the table is empty
            {
                c_TokenTable.Add("ERROR", 0);
                c_TokenTable.Add("RM", 1);
                c_TokenTable.Add("CL", 2);
                c_TokenTable.Add("BD", 3);
                c_TokenTable.Add("CSCI", 4);
                c_TokenTable.Add("ARTH", 5);
                c_TokenTable.Add("ART", 6);
                c_TokenTable.Add("BIO", 7);
                c_TokenTable.Add("CHEM", 8);
                c_TokenTable.Add("FLAN", 9);
                c_TokenTable.Add("LIT", 10);
                c_TokenTable.Add("ENGL", 11);
                c_TokenTable.Add("HIST", 12);
                c_TokenTable.Add("REL", 13);
                c_TokenTable.Add("SOC", 14);
                c_TokenTable.Add("PHIL", 15);
                c_TokenTable.Add("BNR", 16);
                c_TokenTable.Add("MUS", 17);
                c_TokenTable.Add("MATH", 18);
                c_TokenTable.Add("SC", 19);
                c_TokenTable.Add("SN", 20);
                c_TokenTable.Add("SG", 21);
                c_TokenTable.Add("VNY", 22);
                c_TokenTable.Add("RDFD", 23);
                c_TokenTable.Add("DVS", 24);
                c_TokenTable.Add("T", 25);
                c_TokenTable.Add("F", 26);
                c_TokenTable.Add(",", 27);
                c_TokenTable.Add("[", 28);
                c_TokenTable.Add("]", 29);
                c_TokenTable.Add(".", 30);
                c_TokenTable.Add("PHYS", 31);
            }
        } // PopulateHashTable
    }
}
